package com.hzcx.app.simplechat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.chat.contract.ChatShareUserContract;
import com.hzcx.app.simplechat.ui.chat.dialog.ChatShareUserDialog;
import com.hzcx.app.simplechat.ui.chat.event.ChatSearchImgEvent;
import com.hzcx.app.simplechat.ui.chat.event.ChatShareUserEvent;
import com.hzcx.app.simplechat.ui.chat.presenter.ChatShareUserPresenter;
import com.hzcx.app.simplechat.ui.friend.FriendRequestActivity;
import com.hzcx.app.simplechat.ui.friend.FriendSearchActivity;
import com.hzcx.app.simplechat.ui.friend.adapter.FriendAdapter;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.user.UserInfoActivity;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.view.SideLetterBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 分享名片-选择好友界面
 */
public class ChatShareUserActivity extends BaseActivity<ChatShareUserPresenter> implements ChatShareUserContract.View, SideLetterBar.OnLetterChangedListener {

    public static final String INTENT_NICK_NAME = "INTENT_NICK_NAME";
    public static final String INTENT_FROM_TAG = "INTENT_FROM_TAG";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.cons_toolbar)
    ConstraintLayout consToolbar;
    @BindView(R.id.rv_friend)
    RecyclerView rvFriend;
    @BindView(R.id.side_bar)
    SideLetterBar sideBar;
    @BindView(R.id.tv_overlay)
    TextView tvOverlay;

    private List<FriendBean> friendData;
    private FriendAdapter friendAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_chat_share_user;
    }

    @Override
    public void initPresenter() {
        mPresenter = new ChatShareUserPresenter();
    }

    @Override
    public void initView() {
        friendData = new ArrayList<>();
        friendAdapter = new FriendAdapter(friendData);
        rvFriend.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvFriend.setAdapter(friendAdapter);

        sideBar.setOverlay(tvOverlay);

        sideBar.setOnLetterChangedListener(this);

        friendAdapter.setOnFriendListItemOnClickListener((headerPos, contentPos, id, bean) -> {
            if (getIntent().getIntExtra(INTENT_FROM_TAG, 0) == 1) {
                startActivity(new Intent(ChatShareUserActivity.this, UserInfoActivity.class)
                        .putExtra(UserInfoActivity.INTENT_USER_ID, bean.getMember_id()));
            } else {
                EventBus.getDefault().post(new ChatShareUserEvent(getIntent().getIntExtra(INTENT_FROM_TAG, 0), bean));
                finish();
            }
        });

    }

    @Override
    public void initData() {
        addHeaderView();
        mPresenter.getFriendList(this);
    }

    /**
     * 好友列表获取成功
     *
     * @param data
     */
    @Override
    public void friendListResult(List<FriendBean> data) {
        try {
            friendData.clear();
            friendData.addAll(data);
            friendAdapter.notifyDataSetChanged();
            // 改变右侧字母选项
            List<String> list=new ArrayList<>();
            list.add("↑");
            for (FriendBean bean:friendData){
                list.add(bean.getName());
            }
            String[] toBeStored = list.toArray(new String[list.size()]);
            sideBar.setContent(toBeStored);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加头部
     */
    private ConstraintLayout cons_search;

    private void addHeaderView() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_chat_share_user_header, null);
        cons_search = view.findViewById(R.id.cons_search);

        cons_search.setOnClickListener(view1 -> startActivity(new Intent(ChatShareUserActivity.this, FriendSearchActivity.class)
                .putExtra(FriendSearchActivity.INTENT_TAG, getIntent().getIntExtra(INTENT_FROM_TAG, 0))));

        friendAdapter.addHeaderView(view);
    }

    /**
     * 根据字母展示不同位置
     *
     * @param letter
     */
    @Override
    public void onLetterChanged(String letter) {
        if (letter.equals("↑")) {
            rvFriend.scrollToPosition(0);
            return;
        }
        for (int i = 0; i < friendData.size(); i++) {
            if (friendData.get(i) == null) {
                continue;
            }
            if (EmptyUtils.isEmpty(friendData.get(i).getName())) {
                continue;
            }
            if (letter.equals(friendData.get(i).getName())) {
                ((LinearLayoutManager) rvFriend.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                break;
            }
        }
    }

    /**
     * 分享名片
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shareUser(ChatShareUserEvent event) {
        finish();
    }
}
